﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using WebSysCore.JSons;
using WebSysCore;
using WebSysCore.Helpers;
using HSchSchedule.Utils;

namespace HSchSchedule.AjaxPages
{
    public partial class AjaxClass : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod(true)]
        public static string GetGroup()
        {
            int cusId;

            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    List<Group> lstGr = DataBase.GetAllGroup(schId);
                    List<JGroup> lstJGr = new List<JGroup>();
                    foreach (Group g in lstGr)
                    {
                        lstJGr.Add(AssignHelp.GroupToJGroup(g));
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(lstJGr);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }

        }
        [WebMethod(true)]
        public static string GetAvailTeach()
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    List<Teacher> lstATeach = DataBase.GetAvailTeacher(schId);
                    List<JTeacher> lstJATeach = new List<JTeacher>();
                    foreach (Teacher t in lstATeach)
                    {
                        lstJATeach.Add(AssignHelp.TeacherTpJTeacher(t));
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(lstJATeach);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }

        }
        [WebMethod(true)]
        public static string GetAllClass()
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    List<Class> lstCl = DataBase.GetAllClass(schId);
                    List<JDClass> lstJDCl = new List<JDClass>();
                    foreach (Class c in lstCl)
                    {
                        JDClass jdc = new JDClass()
                        {
                            Cl = AssignHelp.ClassToJClass(c),
                            T = AssignHelp.TeacherTpJTeacher(DataBase.GetTeacherById(c.TeacherID)),
                            G = AssignHelp.GroupToJGroup(DataBase.GetGroupOfClass(c))
                        };
                        lstJDCl.Add(jdc);
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(lstJDCl);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }

        }
        [WebMethod(true)]
        public static string GetGVCN2Edit(int classId)
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    List<Teacher> lstTeach = new List<Teacher>();
                    //DataBase.GetG
                    lstTeach.Add(DataBase.GetGVCNOfClass(classId));
                    lstTeach.AddRange(DataBase.GetAvailTeacher(schId));
                    List<JTeacher> lstJTeach = new List<JTeacher>();
                    foreach (Teacher t in lstTeach)
                    {
                        lstJTeach.Add(AssignHelp.TeacherTpJTeacher(t));
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(lstJTeach);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }
        }

        [WebMethod(true)]
        public static string DeleteClass(int classId)
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    //DataBase.
                    DataBase.DeleteClass(classId);
                    return GUIConstants.SUCCESS_RESULT;
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }
        }
        [WebMethod(true)]
        public static string AddClass(JClass jclass)
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    if (ValidateHelp.ValidateClass(jclass))
                    {
                        Class cl = AssignHelp.JClassToClass(jclass);
                        cl.ScheduleInListId = schId;
                        DataBase.InsertClass(cl);
                        return GUIConstants.SUCCESS_RESULT;
                    }
                    else
                    {
                        return GUIConstants.FAIL_RESULT;
                    }
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }
        }
        [WebMethod(true)]
        public static string EditClass(int currentId, JClass jclass)
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    if (ValidateHelp.ValidateClass(jclass) && DataBase.CheckClass(currentId))
                    {
                        Class cl = AssignHelp.JClassToClass(jclass);
                        cl.ScheduleInListId = schId;
                        DataBase.UpdateClass(currentId, cl);
                        return GUIConstants.SUCCESS_RESULT;
                    }
                    else
                    {
                        return GUIConstants.FAIL_RESULT;
                    }
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }
        }
    }
}
